package com.sikulifang.bank.simulation.util;

import com.sikulifang.bank.simulation.constant.CodeConstants;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.io.Serializable;

import static com.sikulifang.bank.simulation.constant.CodeConstants.ERROR;

/**
 * 响应信息主体
 *
 * @param <T>
 * @author lengleng
 */
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@SuppressWarnings("unused")
public class R<T> implements Serializable {

    @Getter
    @Setter
    private int code;

    @Getter
    @Setter
    private String msg;

    @Getter
    @Setter
    private T data;

    public static <T> R<T> ok() {
        return ok(null);
    }

    public static <T> R<T> ok(T data) {
        return ok(data, null);
    }

    public static <T> R<T> ok(T data, String msg) {
        return restResult(data, CodeConstants.SUCCESS, msg);
    }

    public static <T> R<T> failed() {
        return restResult(null, ERROR, null);
    }

    public static <T> R<T> failed(String msg) {
        return restResult(null, ERROR, msg);
    }

    public static <T> R<T> failed(T data) {
        return restResult(data, ERROR, null);
    }

    public static <T> R<T> failed(T data, String msg) {
        return restResult(data, ERROR, msg);
    }

    public static <T> R<T> restResult(T data, int code, String msg) {
        R<T> apiResult = new R<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }

}
